package com.api.common.model.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * 网关请求日志（含风控特征）
 * @TableName gateway_access_log
 */
@TableName(value ="gateway_access_log")
@Data
public class GatewayAccessLog implements Serializable {
    /**
     * 主键ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 请求唯一ID（网关生成的UUID/雪花ID）
     */
    @TableField(value = "request_id")
    private String requestId;

    /**
     * 请求到达网关时间
     */
    @TableField(value = "created_at")
    private Date createdAt;

    /**
     * 用户ID（已登录用户时可用）
     */
    @TableField(value = "user_id")
    private Long userId;

    /**
     * 访问密钥（Access Key），用于鉴权
     */
    @TableField(value = "ak")
    private String ak;

    /**
     * 接口定义ID（关联interface_info表）
     */
    @TableField(value = "interface_info_id")
    private Long interfaceInfoId;

    /**
     * 请求路径（可存原始路径或模板化后的路径）
     */
    @TableField(value = "path")
    private String path;

    /**
     * HTTP方法（GET/POST/PUT/DELETE...）
     */
    @TableField(value = "method")
    private String method;

    /**
     * 请求来源IP地址
     */
    @TableField(value = "ip")
    private String ip;

    /**
     * User-Agent（客户端标识）
     */
    @TableField(value = "ua")
    private String ua;

    /**
     * 请求头数量
     */
    @TableField(value = "header_cnt")
    private Integer headerCnt;

    /**
     * 请求参数数量（query + form）
     */
    @TableField(value = "param_cnt")
    private Integer paramCnt;

    /**
     * URL查询字符串长度
     */
    @TableField(value = "query_size")
    private Integer querySize;

    /**
     * 请求体大小（字节数）
     */
    @TableField(value = "body_size")
    private Integer bodySize;

    /**
     * 下游接口返回的状态码（HTTP code 或业务code）
     */
    @TableField(value = "code")
    private Integer code;

    /**
     * 调用耗时（毫秒）
     */
    @TableField(value = "latency_ms")
    private Integer latencyMs;

    /**
     * 是否成功：1成功，0失败
     */
    @TableField(value = "success")
    private Integer success;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    @Override
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (getClass() != that.getClass()) {
            return false;
        }
        GatewayAccessLog other = (GatewayAccessLog) that;
        return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
            && (this.getRequestId() == null ? other.getRequestId() == null : this.getRequestId().equals(other.getRequestId()))
            && (this.getCreatedAt() == null ? other.getCreatedAt() == null : this.getCreatedAt().equals(other.getCreatedAt()))
            && (this.getUserId() == null ? other.getUserId() == null : this.getUserId().equals(other.getUserId()))
            && (this.getAk() == null ? other.getAk() == null : this.getAk().equals(other.getAk()))
            && (this.getInterfaceInfoId() == null ? other.getInterfaceInfoId() == null : this.getInterfaceInfoId().equals(other.getInterfaceInfoId()))
            && (this.getPath() == null ? other.getPath() == null : this.getPath().equals(other.getPath()))
            && (this.getMethod() == null ? other.getMethod() == null : this.getMethod().equals(other.getMethod()))
            && (this.getIp() == null ? other.getIp() == null : this.getIp().equals(other.getIp()))
            && (this.getUa() == null ? other.getUa() == null : this.getUa().equals(other.getUa()))
            && (this.getHeaderCnt() == null ? other.getHeaderCnt() == null : this.getHeaderCnt().equals(other.getHeaderCnt()))
            && (this.getParamCnt() == null ? other.getParamCnt() == null : this.getParamCnt().equals(other.getParamCnt()))
            && (this.getQuerySize() == null ? other.getQuerySize() == null : this.getQuerySize().equals(other.getQuerySize()))
            && (this.getBodySize() == null ? other.getBodySize() == null : this.getBodySize().equals(other.getBodySize()))
            && (this.getCode() == null ? other.getCode() == null : this.getCode().equals(other.getCode()))
            && (this.getLatencyMs() == null ? other.getLatencyMs() == null : this.getLatencyMs().equals(other.getLatencyMs()))
            && (this.getSuccess() == null ? other.getSuccess() == null : this.getSuccess().equals(other.getSuccess()));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
        result = prime * result + ((getRequestId() == null) ? 0 : getRequestId().hashCode());
        result = prime * result + ((getCreatedAt() == null) ? 0 : getCreatedAt().hashCode());
        result = prime * result + ((getUserId() == null) ? 0 : getUserId().hashCode());
        result = prime * result + ((getAk() == null) ? 0 : getAk().hashCode());
        result = prime * result + ((getInterfaceInfoId() == null) ? 0 : getInterfaceInfoId().hashCode());
        result = prime * result + ((getPath() == null) ? 0 : getPath().hashCode());
        result = prime * result + ((getMethod() == null) ? 0 : getMethod().hashCode());
        result = prime * result + ((getIp() == null) ? 0 : getIp().hashCode());
        result = prime * result + ((getUa() == null) ? 0 : getUa().hashCode());
        result = prime * result + ((getHeaderCnt() == null) ? 0 : getHeaderCnt().hashCode());
        result = prime * result + ((getParamCnt() == null) ? 0 : getParamCnt().hashCode());
        result = prime * result + ((getQuerySize() == null) ? 0 : getQuerySize().hashCode());
        result = prime * result + ((getBodySize() == null) ? 0 : getBodySize().hashCode());
        result = prime * result + ((getCode() == null) ? 0 : getCode().hashCode());
        result = prime * result + ((getLatencyMs() == null) ? 0 : getLatencyMs().hashCode());
        result = prime * result + ((getSuccess() == null) ? 0 : getSuccess().hashCode());
        return result;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", id=").append(id);
        sb.append(", requestId=").append(requestId);
        sb.append(", createdAt=").append(createdAt);
        sb.append(", userId=").append(userId);
        sb.append(", ak=").append(ak);
        sb.append(", interfaceInfoId=").append(interfaceInfoId);
        sb.append(", path=").append(path);
        sb.append(", method=").append(method);
        sb.append(", ip=").append(ip);
        sb.append(", ua=").append(ua);
        sb.append(", headerCnt=").append(headerCnt);
        sb.append(", paramCnt=").append(paramCnt);
        sb.append(", querySize=").append(querySize);
        sb.append(", bodySize=").append(bodySize);
        sb.append(", code=").append(code);
        sb.append(", latencyMs=").append(latencyMs);
        sb.append(", success=").append(success);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }
}